package com.example.controller;

import com.example.pojo.Dept;
import com.example.pojo.Result;
import com.example.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    //查询所有部门信息
    @RequestMapping
    public Result list() {
        //System.out.println("查询所有部门信息");
        log.info("查询所有部门信息");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }


    //删除部门信息
    @DeleteMapping
    public Result delete(Integer id) {
        System.out.println("根据id删除部门信息：" + id);
        log.info("根据id删除部门信息：{}", id);
        deptService.deleteByID(id);
        return Result.success();
    }

    //新增部门信息
    @PostMapping
    public Result post(@RequestBody Dept dept) {
        //System.out.println("新增部门信息：" + dept);
        log.info("新增部门信息：{}", dept);
        deptService.insert(dept);
        return Result.success();
    }


    //查询回显
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        //System.out.println("查询部门的信息：" + id);
        log.info("查询部门的信息：{}", id);
        Dept dept = deptService.getByID(id);
        return Result.success(dept);
    }

    //修改部门信息
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        //System.out.println("修改部门的信息：" + dept);
        log.info("修改部门的信息：{}", dept);
        deptService.update(dept);
        return Result.success();
    }
}
